package com.ctgu.elasticsearch.controller;

import com.ctgu.elasticsearch.service.HotelService;
import com.ctgu.elasticsearch.service.UserService;
import com.ctgu.elasticsearch.utils.ApiResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @ ClassName HotelController
 * @ Author Alex2
 * @ Date 2025/8/24 19:23
 **/
@RestController
@RequestMapping("/api/hotel")
public class HotelController {

    private final HotelService hotelService;

    public HotelController(HotelService hotelService) {
        this.hotelService = hotelService;
    }

    /**
     * @ Author: Alex
     * @ Description: 插入
     */

    // http://localhost:8080/api/hotel/insertHotel?name=维也纳国际酒店&address=宝安大道34号&price=400&brand=维也纳&city=深圳&longitude=116.397450&latitude=39.908860&isAD=true
    // http://localhost:8080/api/hotel/insertHotel?name=汉庭国际酒店&address=西乡26号&price=600&brand=汉庭酒店&city=深圳&longitude=117.763275&latitude=38.320495&isAD=false
    @GetMapping("/insertHotel")
    public ApiResult insertHotel(@RequestParam(value = "name") String name,
                                 @RequestParam(value = "address") String address,
                                 @RequestParam(value = "price") Integer price,
                                 @RequestParam(value = "brand") String brand,
                                 @RequestParam(value = "city") String city,
                                 @RequestParam(value = "longitude") BigDecimal longitude,
                                 @RequestParam(value = "latitude") BigDecimal latitude,
                                 @RequestParam(value = "isAD") Boolean isAD) throws IOException {
        return hotelService.insertHotel(name, address, price, brand, city, longitude, latitude, isAD);
    }

    /**
     * @ Author: Alex
     * @ Description: 根据关键字查询
     */
    // http://localhost:8080/api/hotel/keywordSearch?keyword=维也纳
    @GetMapping("keywordSearch")
    public ApiResult keywordSearch(@RequestParam(value = "keyword") String keyword,
                                   @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "orderBy", required = false, defaultValue = "name") String orderBy,
                                   @RequestParam(value = "sortType", required = false, defaultValue = "asc") String sortType
    ) throws IOException {
        return hotelService.keywordSearch(keyword, pageNum, pageSize, orderBy, sortType);
    }

    /**
     * @ Author: Alex
     * @ Description: 筛选
     */
    // http://localhost:8080/api/hotel/filter?brand=维也纳&city=深圳&maxPrice=1000&minPrice=100
    // maxPrice 和 minPrice 应该同时出现或同时不出现
    @GetMapping("filter")
    public ApiResult filter(@RequestParam(value = "brand", required = false) String brand,
                            @RequestParam(value = "address", required = false) String address,
                            @RequestParam(value = "city", required = false) String city,
                            @RequestParam(value = "maxPrice", required = false) Integer maxPrice,
                            @RequestParam(value = "minPrice", required = false) Integer minPrice,
                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                            @RequestParam(value = "orderBy", required = false, defaultValue = "name") String orderBy,
                            @RequestParam(value = "sortType", required = false, defaultValue = "asc") String sortType) throws IOException {
        return hotelService.filter(brand, address, city, maxPrice, minPrice, pageNum, pageSize, orderBy, sortType);
    }

    /**
     * @ Author: Alex
     * @ Description: 根据距离排序
     */
    // http://localhost:8080/api/hotel/orderByDistance?longitude=116.0&latitude=39.0
    @GetMapping("orderByDistance")
    public ApiResult orderByDistance(@RequestParam(value = "longitude", required = false) BigDecimal longitude,
                                     @RequestParam(value = "latitude", required = false) BigDecimal latitude,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "orderBy", required = false, defaultValue = "name") String orderBy,
                                     @RequestParam(value = "sortType", required = false, defaultValue = "asc") String sortType) throws IOException {
        return hotelService.orderByDistance(longitude, latitude, pageNum, pageSize, orderBy, sortType);
    }

    /**
     * @ Author: Alex
     * @ Description: 聚合查询
     */
    // http://localhost:8080/api/hotel/aggregateSearch?city=深圳
    @GetMapping("aggregateSearch")
    public ApiResult aggregateSearch(@RequestParam(value = "brand", required = false) String brand,
                                     @RequestParam(value = "city", required = false) String city,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "orderBy", required = false, defaultValue = "name") String orderBy,
                                     @RequestParam(value = "sortType", required = false, defaultValue = "asc") String sortType) throws IOException {
        return hotelService.aggregateSearch(brand, city, pageNum, pageSize, orderBy, sortType);
    }

    /**
     * @ Author: Alex
     * @ Description: 自动补全查询
     */
    // http://localhost:8080/api/hotel/pinyinSearch?name=wei
    @GetMapping("pinyinSearch")
    public ApiResult pinyinSearch(@RequestParam(value = "name") String name) throws IOException {
        return hotelService.pinyinSearch(name);
    }
}